package
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;

	public class Background extends Sprite
	{
		private var background:Sprite = new Sprite();
		private var newX:int = 0;
		private var newY:int = 0;
		private var newWidth:int = 480;
		private var newHeight:int = 272;
		private var newRound:int = 8;
		
		private var imageUrl:String = "asset/Boxee_Green.jpg";//"http://thinktube.net/project/pspnav/asset/Boxee_Green.jpg";
		private var loader:Loader = new Loader();
		
		public function Background(_stageWidth:int,_stageHeight:int)
		{
			trace(_stageWidth +" "+ _stageHeight);
			newX = (_stageWidth - newWidth) / 2;
			newY = (_stageHeight - newHeight) / 2;
			var request:URLRequest = new URLRequest(imageUrl);
			loader.load(request);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,draw);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
		}
		
		private function draw(event:Event):void
		{
			var bitmapData:BitmapData = new BitmapData(newWidth,newHeight,false);
			bitmapData.draw(loader);
			var matrix:Matrix = new Matrix();
			background.graphics.beginBitmapFill(bitmapData,matrix,true);
			background.graphics.drawRoundRectComplex(newX,newY,newWidth,newHeight,newRound,newRound,newRound,newRound);
			background.graphics.endFill();
			this.addChild(background);
		}
		
		
		private function ioErrorHandler(event:Event):void
		{
			trace("Unable to load image"+imageUrl);
		}
	}
}